
mfn = './emaf_paper/save_files/morph_distances.sav'
IF ~ FILE_TEST(mfn) THEN run_morph_matching,/drivemorph,/silent
restore,mfn,/ver

;; Restrict consideration to "well-constrained" sources...
cind = where(constr_ml)
duse  = duse[cind,*]
dbar = dbar[cind,*]

fullduse   = duse
fulldbar  = dbar
fullpchoo = pchoo

openw,lun,'/d1/BGPS/bgps_svn/papers/IRDC_prior/tables/grs_table.tex',/GET_LUN

;; Table Header:
printf,lun,'%% GRS Matching Statistics as a function of angular separation'
printf,lun,''
printf,lun,'\begin{deluxetable}{cccccccccccc}'
printf,lun,'  \tablecolumns{12}'
printf,lun,'  \tablewidth{0pc}'
printf,lun,'  \tabletypesize{\small}'
printf,lun,'  \tablecaption{GRS Matching Stats as a Function of Angular Separation\label{table:grs_comp}}'
printf,lun,'  \tablehead{'
printf,lun,'    \colhead{} & \multicolumn{2}{c}{All Sources} & \colhead{} &'
printf,lun,'    \multicolumn{2}{c}{$C \geq 0.1$} & \colhead{} &'
printf,lun,'    \multicolumn{2}{c}{$C \geq 0.15$} & \colhead{} &'
printf,lun,'    \multicolumn{2}{c}{$C \geq 0.2$} \\'
printf,lun,'    \cline{2-3} \cline{5-6} \cline{8-9} \cline{11-12}'
printf,lun,'    \colhead{Ang. Sep.} & \colhead{$N_\mathrm{tot}$} & '
printf,lun,'    \colhead{Success} & \colhead{} &'
printf,lun,'    \colhead{$N_\mathrm{tot}$} & \colhead{Success} & \colhead{} &'
printf,lun,'    \colhead{$N_\mathrm{tot}$} & \colhead{Success} & \colhead{} &'
printf,lun,'    \colhead{$N_\mathrm{tot}$} & \colhead{Success} \\'
printf,lun,'    \colhead{(\arcmin)} & \colhead{} & \colhead{(\%)} & '
printf,lun,'    \colhead{} & \colhead{} & \colhead{(\%)} &'
printf,lun,'    \colhead{} & \colhead{} & \colhead{(\%)} &'
printf,lun,'    \colhead{} & \colhead{} & \colhead{(\%)}'
printf,lun,'  }'
printf,lun,'  \startdata'

fal = fltarr(10)
f10 = fltarr(10)
f15 = fltarr(10)
f20 = fltarr(10)


FOR angsep=1.,10. DO BEGIN
   
   add_grs_info, ANGSEP=angsep
   undefine,emaf
   restore,'./emaf_paper/bgps_emaf.sav',/ver
   emaf = emaf[cind]
   
   duse   = fullduse
   dbar  = fulldbar
   pchoo = fullpchoo
   
   ;; SELECT ONLY GRS-ASSOCIATED SOURCES
   ind   = where(emaf.grs, n)
   emaf  = emaf[ind]
   duse   = duse[ind,*]
   dbar  = dbar[ind,*]
   phcoo = pchoo[ind]
   
   c10 = where(emaf.c GE 0.10, n10)
   c15 = where(emaf.c GE 0.15, n15)
   c20 = where(emaf.c GE 0.20, n20)
   
   
   ical = where(abs(duse[*,0]-emaf.grs_dist) GT 1., ncal)
   ic10 = where(abs(duse[c10,0]-emaf[c10].grs_dist) GT 1., nc10)
   ic15 = where(abs(duse[c15,0]-emaf[c15].grs_dist) GT 1., nc15)
   ic20 = where(abs(duse[c20,0]-emaf[c20].grs_dist) GT 1., nc20)
   
   
   printf,lun,string(fix(angsep),n,(1.-float(ncal)/float(n))*100.,$
                     n10,(1.-float(nc10)/float(n10))*100.,$
                     n15,(1.-float(nc15)/float(n15))*100.,$
                     n20,(1.-float(nc20)/float(n20))*100.,$
                     format="('$\leq ',I0,'$ & ',I0,' & ',F0.1,' & & ',I0,' & ',F0.1,' & & ',I0,' & ',F0.1,' & & ',I0,' & ',F0.1,' \\')")
   
   fal[fix(angsep-1)] = (1.-float(ncal)/float(n))
   f10[fix(angsep-1)] = (1.-float(nc10)/float(n10))
   f15[fix(angsep-1)] = (1.-float(nc15)/float(n15))
   f20[fix(angsep-1)] = (1.-float(nc20)/float(n20))
   
   eyes = ncal NE 0 ? emaf[ical] : 0.
   save,emaf,eyes,duse,pchoo,filename='./emaf_paper/save_files/grs_stat_invx.sav',/ver
   
ENDFOR

;; Table footer:
printf,lun,'  \enddata'
printf,lun,'  \tablecomments{In the GRS catalog \citep{RomanDuval:2009}, median $\Delta\ell = 20.4\arcmin$, and median $\Delta b = 17.4\arcmin$.}'
printf,lun,'\end{deluxetable}'

close,lun
free_lun,lun

angsep = findgen(10)+1.

myps,'./emaf_paper/plots/grs_ang_sep.eps'

cgPlot,angsep,fal,/xst,yr=[0.8,1.0],thick=3,ytit='Distance Matching Fraction',$
       xtit='GRS - BGPS Cataloged Angular Separation [arcmin]',/nodata,$
       charsize=1.0,xminor=2
       
vline,/h,0.9,thick=3,color=cgColor('ORG5')

colors = ['Opposite','Dodger Blue','Forest Green','Firebrick']

cgOplot,angsep,fal,color=colors[0],thick=3,psym=-16
cgOplot,angsep,f10,color=colors[1],thick=3,psym=-16
cgOplot,angsep,f15,color=colors[2],thick=3,psym=-16
cgOplot,angsep,f20,color=colors[3],thick=3,psym=-16
al_legend,box=0,/bottom,/left,color=colors,thick=3,psym=-16,$
          ['All','C '+cgSymbol('geq')+' '+$
           string(findgen(3)/20.+0.1,format="(F0.2)")]
                                                 

myps,/done
END
